package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.crm.constants.DictConstants;
import com.yu.crm.domain.pojo.CrmPointPatrol;
import com.yu.crm.domain.pojo.CrmRoutePatrol;
import com.yu.crm.mapper.CrmPointPatrolMapper;
import com.yu.crm.service.ICrmPointPatrolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 巡更点 服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-09-23
 */
@Service
public class CrmPointPatrolService extends ServiceImpl<CrmPointPatrolMapper, CrmPointPatrol> implements ICrmPointPatrolService {

    @Autowired
    CrmRoutePatrolService routePatrolService;

    @Override
    public List<CrmPointPatrol> findByRoutePatrolId(Long routePatrolId) {
        QueryWrapper<CrmPointPatrol> wrapper = new QueryWrapper<>();
        wrapper.eq("route_patrol_id", routePatrolId);
        wrapper.eq("patrol_type", DictConstants.QrOrVideo.QR);
        wrapper.orderByDesc("sort");
        return this.list(wrapper);
    }

    @Override
    public void pointUp(Long id) {
        CrmPointPatrol pointPatrol = this.getById(id);
        //查找当前id的上一个节点，如果没用不做操作。若有交换当前节点和上一节点的ID
        CrmPointPatrol prePointPatrol = baseMapper.findPrePointById(pointPatrol.getRoutePatrolId(), pointPatrol.getSort());
        if(DataHelpers.isNotEmpty(prePointPatrol)) {
            int tmp = 0;
            tmp = prePointPatrol.getSort();
            prePointPatrol.setSort(pointPatrol.getSort());
            pointPatrol.setSort(tmp);
        }
        List<CrmPointPatrol> list = new ArrayList<>();
        list.add(pointPatrol);
        list.add(prePointPatrol);
        this.saveOrUpdateBatch(list);
    }

    @Override
    public void pointDown(Long id) {
        CrmPointPatrol pointPatrol = this.getById(id);//当前节点
        CrmPointPatrol postPointPatrol = baseMapper.findPostPointById(pointPatrol.getRoutePatrolId(), pointPatrol.getSort());//
        if(DataHelpers.isNotEmpty(postPointPatrol)) {
            Integer tmp = 0;
            tmp = postPointPatrol.getSort();
            postPointPatrol.setSort(pointPatrol.getSort());
            pointPatrol.setSort(tmp);
        }
        List<CrmPointPatrol> list = new ArrayList<>();
        list.add(pointPatrol);
        list.add(postPointPatrol);
        this.saveOrUpdateBatch(list);
    }

    @Override
    public void insert(CrmPointPatrol pointPatrol) {
        //如果该路线上没有其他节点，把节点+1
        pointPatrol.setSort(getMaxSort(pointPatrol.getRoutePatrolId()));
        this.save(pointPatrol);
    }


    public int getMaxSort(Long routePatrolId) {
        List<CrmPointPatrol> list = findByRoutePatrolId(routePatrolId);
        if (DataHelpers.isNotEmpty(list)&&list.size()>0) {
            return list.get(0).getSort()+1;
        }
        return 0;
    }

}
